3. The YouTD API

The API of YouTD is a set off vJASS structs and functions to help you create beautiful towereffects with very few lines of code.

Before you start reading it remember one thing: This documentation is very long since it tries to cover any aspect of the API. However you don't need all parts of it for your tower. For example, if your tower just attacks and casts a dummy spell, you can basically skip the chapters about Buffs, Auras, Autocasts and Projectiles.You can read them to understand everything, but don't get frustrated, just skip them if you don't need them.

Another thing you should always remember: There is a commented version of the API interfaces in the Content Creation Map. So, if you want to know something, always first check the comments there.

Here is an overview of the YouTD structs, as an UML class diagram.

So the main type for creeps and towers is the type Unit. It is a wrapper for a warcraft 3 unit and offers many method to get information about the unit or alter it. Next thing you should know, is that every tower has an owner of type Playor (which is a wrapping struct for a player) and ever Playor has a team. You can use these structs to get/set information about the owning player and its team. For casting dummy spells, the system first defines the dummy and ability type by creating a struct of type Cast. This struct can then create SpellDummies. The buff engine mainly works with the type BuffType. It is a struct type that specify the type of a buff. You create it on map init and fill it with all the stuff the buff should do (icon, effects, modifications, events) and then you can call the applyBuff method to create a buff of this type on a unit. The projectile engine (struct Projectile) allows you to create moving visible dummy units that can do different stuff and react to different events. That was the YouTD API in a few sentences, I know that it is not understandable yet, but whenever you lose the overview over what does what, check this chapter again. The next chapters will cover the different details of the API that it gets understandable.

Before we start talking about Projectiles, Buffs and Dummies, you should understand what you can do with the basic data structures for units and players, because you will need them almost everywhere.

Always devide between Unit with an upper case U and unit. The one is a struct member, the other one a Warcraft3 unit. The struct Unit wraps a Warcraft3 unit. You can get the wrapped unit by calling this method for any Unit:

method getUnit takes nothing returns unit
This struct has two important structs derived from it: Creep and Tower. They offer additional functionalities which are special to creeps and towers. So where do you get Units from? First, every event handling function looks like this:
function onXXX takes Tower tower returns nothing
As you see, it takes a parameter tower of type Tower. This is your tower. Since it is derived from the type Unit, it allows you to use all methods of Unit. In addition, for targeted events you can call the static method of the struct Event:
struct Event static method getTarget takes nothing returns Unit
It gets you the target of this event. So for example, for the onKill, onAttack, onDamage events, it gets you the killed/attacked/damaged unit. You can get units by picking units in range or something like that. How do you get the struct wrapping them? Very simple: The struct index is saved in their custom value, so you can just call GetUnitUserData(unit) to get a Unit. If the custom value is zero, then the unit is already dead or has no struct attached to it. In both cases, you should discard the unit. So getting a Unit from a unit can be done like that:
local Unit u = GetUnitUserData(YOURUNIT)
Here, YOURUNIT must be a variable of type unit. Now you can also check if the unit is a tower or a creep. For some events, you can just cast the Unit to Creep or Tower because it is clear what it is. Example: The Event.getTarget() function will always return a creep if used on an OnAttack event (since towers don't attack each other). If it is unclear if the Unit is a Tower or a Creep, you have two possibilities. Either you can call these methods of type Unit:
struct Unit method isATower takes nothing returns boolean method isACreep takes nothing returns boolean
They return true if the Unit is a Tower/Creep. You can also call these functions for a unit to determine what it is:
function isCreepAndAlive takes unit u returns boolean function isTower takes unit u returns boolean function isDummy takes unit u returns boolean
Code example:
local Unit u = GetUnitUserData(YOURUNIT) if isTower(YOURUNIT) then call BJDebugMsg("This unit is a tower") endif if u.isATower() then call BJDebugMsg("This Unit (with capital U!) is a tower") endif

After having learned how to aquire a unit and test what kind it is, let's learn what you can do with it. There are many methods you can use. Here are just some of them. For a complete list, just check the commented API in the Content Creation Map:

struct Unit //Returns the number of kills this unit did method getKills takes nothing returns integer //Returns the owning player of this unit method getOwner takes nothing returns Playor //Calculates a spell crit chance for this tower with bonusChance additional chance to crit // and bonusDamage anditional damage on crit method calcSpellCrit takes real bonusChance, real bonusDamage returns real //Calculate a chance for this unit's with chance percent. //Use this function whenever you want something to happen for this unit on a percent base //example: use .calcChance(0.5) to get a 50% chance //==> will return true on 50% of all invokations //however, note that the given value will be modified by the unit's chance bonuses method calcChance takes real chance returns boolean //Returns the buff of BuffType bt that the unit has //If the unit has no buff of this buff type, 0 will be returned //So you can use uc.getBuffOfType(...) != 0 to check if a unit has a specific buff method getBuffOfType takes BuffType bt returns Buff ...

These two structs represent wrappers for a player and his team. You can get Playors by using the getOwner method of Units. You can get his team by calling his getTeam method. Playor and Team offer some methods to display (floating) text to the playor/team, play sounds for them, alter their gold/income and so on.

Here are some methods for the struct Playor (again, a complete list can be found in the API itself):

struct Playor //Creates a floating text for this player on a Unit, uses standard values //whichText: the text to be displayed //whichUnit: the unit where the text is displayed //red,green,blue: colors of your text, between 0 and 255 method displayFloatingText takes string whichText, Unit whichUnit, integer red, integer green, integer blue returns nothing //Gets the team this player belongs to method getTeam takes nothing returns Team //Gets the number of tower this player has method getNumTower takes nothing returns integer //modifies the interest rate of this player by delta. //Delta is percent based, so 0.01 = +1% interest rate method modifyInterestRate takes real delta returns nothing //Gives gold to this player. You can specify a unit for this, if you do so then //there will be an effect at the unit's position (if showEffect is true) and a //golden floating text that shows the value of gold gained (if showText is true) //If you don't want personalized effect stuff, just set u to null //You can use a negative value to substract gold (then the floating text will be red instead of gold) method giveGold takes integer value, unit u, boolean showEffect, boolean showText returns nothing ...

and here are some methods of the struct Team:

struct Team //Gets the size of this team, i.e. how many NON-LEFT(!) players it has method getSize takes nothing returns integer //Gets the level in which this team currently is method getLevel takes nothing returns integer //Retrievs the percentage of lifes this team has //Note that this is really percentage, so 40 = 40% //(in opposite to many other functions that return a real value where 0.4 is 40%) method getLivesPercent takes nothing returns integer //Gives gold to all players of this team. You can specify a unit for this, if you do so then //there will be an effect at the unit's position (if showEffect is true) and a //golden floating text that shows the value of gold gained (if showText is true) //If you don't want personalized effect stuff, just set u to null //You can use a negative value to substract gold (then the floating text will be red instead of gold) method giveGoldToEachPlayer takes integer value, unit u, boolean showEffect, boolean showText returns nothing ...

Now you have the basic knowledge of what can be done where, how to acquire Units, their owners and their teams.

Let's start with the easiest things you can do to alter a Tower / Creep. Modifying its values like speed, damage, armor and so on. The YouTD API allows you to alter many other advanced things like critical strike chance,item drop chance.

The most simple method to alter a Unit's stats is this one:

struct Unit //Modifies a property modId by value method modifyProperty takes integer modId, real value returns nothing
modId is an integer which can be one of these constants (excerpt from library Modifier). All modifications are commented, so you know what you can do. As you see, you can alter ALMOST everything you can imagine:
globals //************************************************************** //** This is a list of possible modIds which can be passed to ** //** the Modifier.addModification function ** //** ALWAYS USE THESE CONSTANTS WHEN INVOKING THIS FUNCTION ** //************************************************************** //**************************************** //** TOWER MODIFICATIONS ** //** The following modifications only ** //** make sense when applied to a tower ** //**************************************** //== DAMAGE_TO TABLE MODIFICATIONS == constant integer MOD_DMG_TO_MASS = 0 //Mass units (very unhealthy) constant integer MOD_DMG_TO_NORMAL = 1 //Normal units (the ones that spawn in normal levels) constant integer MOD_DMG_TO_CHAMPION = 2 //Champions are larger units that spawn with normal units constant integer MOD_DMG_TO_BOSS = 3 //Bosses are even mightier than champions (the will probably spawn alone) constant integer MOD_DMG_TO_AIR = 4 //Air levels constant integer MOD_DMG_TO_UNDEAD = 10 //Undead units constant integer MOD_DMG_TO_MAGIC = 11 //Magical units constant integer MOD_DMG_TO_NATURE = 12 //Nature units constant integer MOD_DMG_TO_ORC = 13 //Orcish units constant integer MOD_DMG_TO_HUMANOID = 14 //Humanoid units //== OFFENSIVE MODIFICATIONS == //Modifies the chance that attacks of this unit will hit critical //percent based so 0.01 = 1% crit chance //Note that every unit starts with a basic crit chance of 1% constant integer MOD_ATK_CRIT_CHANCE = 20 //Modifies the damage that critical attacks of this unit do //percent based so 0.01 = 1% crit damage //Note that every unit starts with a basic crit damage of 150% (+50%) constant integer MOD_ATK_CRIT_DAMAGE = 21 //Modifies the multicrit value of this unit. Multicrit is the chance that a //critical strike on attack crits again. The basic multicritValue of each unit //is 1, so a unit can crit only once. //If you increment its value, then it can crit two times, if you decrease it to //zero, than the unit cannot crit at all constant integer MOD_MULTICRIT_COUNT = 22 //Modifies the chance that spells of this unit will hit critical //percent based so 0.01 = 1% crit chance //Note that every unit starts with a basic crit chance of 1% constant integer MOD_SPELL_CRIT_CHANCE = 23 //Modifies the damage that critical spells of this unit do //percent based so 0.01 = 1% crit damage //Note that every unit starts with a basic crit damage of 150% (+50%) constant integer MOD_SPELL_CRIT_DAMAGE = 24 //Modifies the damage of all spells cast by this unit //percent based so 0.01 = 1% spell damage constant integer MOD_SPELL_DAMAGE_DEALT = 25 //Increases the chance for creeps killed by this tower to drop an item //percent based so 0.01 = +1% better chance to drop an item constant integer MOD_ITEM_CHANCE_ON_KILL = 26 //Increases the quality of items dropped by creeps which are killed by this unit //percent based so 0.01 = +1% better quality constant integer MOD_ITEM_QUALITY_ON_KILL = 27 //Modifies the attackspeed of the unit on a percentage base, //1 = 1% attackspeed, 20.0 = 20% attackspeed and so on constant integer MOD_ATTACKSPEED = 30 //Modifies the base damage of this unit (the white numbers) //1.0 = +1 damage constant integer MOD_DAMAGE_BASE = 31 //Modifies the base damage of this unit (the white numbers) //percent based so 0.01 = 1% damage constant integer MOD_DAMAGE_BASE_PERC = 32 //Modifies the add damage of this unit (the green numbers) //1.0 = +1 damage constant integer MOD_DAMAGE_ADD = 33 //Modifies the add damage of this unit (the green numbers) //percent based so 0.01 = 1% damage constant integer MOD_DAMAGE_ADD_PERC = 34

//**************************************** //** CREEP MODIFICATIONS ** //** The following modifications only ** //** make sense when applied to a creep ** //**************************************** //== DAMAGE_FROM TABLE MODIFICATIONS == constant integer MOD_DMG_FROM_FIRE = 40 //Damage from fire towers constant integer MOD_DMG_FROM_BLIZZARD = 41 //Damage from blizzard towers constant integer MOD_DMG_FROM_NATURE = 42 //Damage from nature towers constant integer MOD_DMG_FROM_ASTRAL = 43 //Damage from astral towers constant integer MOD_DMG_FROM_DARKNESS = 44 //Damage from darkness towers constant integer MOD_DMG_FROM_MULTI_ELEMENTAL = 45 //Damage from multi elemental towers //== DEFENSIVE MODIFICATIONS == //Modifies the experience this unit grants upon death //percent based so 0.01 = +1% exp constant integer MOD_EXP_GRANTED = 60 //Modifies the bounty this unit grants upon death //percent based so 0.01 = +1% bounty constant integer MOD_BOUNTY_GRANTED = 61 //Modifies the spell damage this unit receives //percent based so 0.01 = +1% spell damage received constant integer MOD_SPELL_DAMAGE_RECEIVED = 62 //Modifies the attack damge this unit receives //percent based so 0.01 = +1% attack damage received constant integer MOD_ATK_DAMAGE_RECEIVED = 63 //Increases the chance for this creeps to drop an item on death //percent based so 0.01 = +1% better chance to drop an item constant integer MOD_ITEM_CHANCE_ON_DEATH = 64 //Increases the quality of items dropped by this creep on death //percent based so 0.01 = +1% better quality constant integer MOD_ITEM_QUALITY_ON_DEATH = 65 //Modifies the hitpoints of this unit //1.0 = +1 hitpoint constant integer MOD_HP = 70 //Modifies the hitpoints of this unit //percent based so 0.01 = +1% hitpoints constant integer MOD_HP_PERC = 71 //Modifies the hitpoint regeneration of this unit //1.0 = +1 hitpoint / sec regeneration constant integer MOD_HP_REGEN = 72 //Modifies the hitpoint regeneration of this unit //percent based so 0.01 = +1% hitpoint regeneration constant integer MOD_HP_REGEN_PERC = 73 //Modifies the armor //1.0 = +1 armor constant integer MOD_ARMOR = 74 //Modifies the armor //percent based so 0.01 = +1% armor constant integer MOD_ARMOR_PERC = 75 //**************************************** //** GENERAL MODIFICATIONS ** //** The following modifications ** //** make sense on creeps and towers ** //**************************************** //== MISC MODIFICATIONS == //Modifies the triggerchances of this unit //i.e.: Whenever the calcChance function is called for this unit it has bonus percents to return true //percent based so 0.01 = +1% trigger chances constant integer MOD_TRIGGER_CHANCES = 80 //Modifies buff durations for buffs cast by this unit //percent based so 0.01 = +1% duration constant integer MOD_BUFF_DURATION = 81 //Modifies buff durations for debuffs cast ONTO this unit //percent based so -0.01 = -1% duration constant integer MOD_DEBUFF_DURATION = 82 //Modifies the bounty this unit receives upon killing //percent based so 0.01 = +1% bounty constant integer MOD_BOUNTY_RECEIVED = 83 //Modifies the experience this unit receives upon killing //percent based so 0.01 = +1% experience constant integer MOD_EXP_RECEIVED = 84 //Modifies the mana of this unit //1.0 = +1 mana constant integer MOD_MANA = 90 //Modifies the mana of this unit //percent based so 0.01 = +1% mana constant integer MOD_MANA_PERC = 91 //Modifies the mana regeneration of this unit //1.0 = +1 mana / second regeneration constant integer MOD_MANA_REGEN = 92 //Modifies the mana regeneration of this unit //percent based so 0.01 = +1% mana regeneration constant integer MOD_MANA_REGEN_PERC = 93 //Modifies the movespeed of this unit //percent based so 0.01 = +1% movespeed constant integer MOD_MOVESPEED = 94 endglobals

Using the modifyProperty you can directly modify a single property. This is not the only way to alter a unit's properties. For example, buffs can modify unit properties, too. If only the modifyProperty method existed, you had to modify all properties whenever that buff is created and modify them with their negative value whenever the buff is removed. YouTD offers a much more simple way to alter stuff like that, where the engine does the add and remove stuff for you: Modifiers.

A modifier is basically a set of modifications, which are also level dependant, that means they can get upgraded automatically if the tower/buff gains levels. This way you could make a modifier that gets stronger the higher the towers level is. You create a modifier using the create method:

struct Modifier //Creates a new damage modifier that doesn't modify anything yet static method create takes nothing returns Modifier
As you see, the method takes nothing and creates a modifier that doesn't modify anything yet. Now we can add modifications:
struct Modifier method addModification takes integer modId, real baseValue, real levelAdd returns nothing
This method adds a modification to this modifier. modId is again one of the above mentioned constants which specifies which property to modify. BaseValue is the value that is modified for a level 0 tower/buff. levelAdd is the value that is added for every level. You can add as many modifications as you wish to a modifier. Example:
local Modifier myMod = Modifier.create() call myMod.addModification(MOD_ATK_CRIT_CHANCE,0.1,0.01)
This would create a modifier that modifies a tower's critical strike chance by 10% (0.1) on level 0 and +1% per level. So for a level 15 tower, this would give 25% more critical strike chance. But this modifier is on no tower yet. It can be added to buffs (but that will be covered in the chapter on buffs). It can also be added to a Unit directly using this method:
struct Unit method addModifier takes Modifier m returns ListElement
This adds a modifier to the unit's modifier list and returns a reference to it. Note that the modifier's strength is automatically adjusted with the unit's level. If the unit gains a level also this modification's strength's will be adjusted. If you want to, you can save this reference and hand it to the method:
struct Unit method removeModifier takes ListElement l returns nothing
which will remove the modifier from this unit.

So if we have a Tower myTower, we could add that modifier to it:

local ListElement modReference = myTower.addModifier(myModifier) call TriggerSleepAction(60.0) call myTower.removeModifier(modReference)
This would add the critical strike modifier to the tower and remove it again after one minute.

However the methods add/removeModifier should only be used in special cases. Most of the time you just add a modifier to a buff. Then the modifier will be on the unit as long as the unit has the buff. You don't have to care about adding and removing it than. This is covered in the chapter on buffs.

As I already told you, towers shouldn't cast their spells themselves. Always, a dummy should do this. Any dummy spell is represented by an instance of the struct Cast. Casting those spells takes TWO steps:

  • Creating an instance of Cast during map initialization (in an init function of your Header library!). This specifies things that stay the same, for every instance of this dummy spell.
  • Casting the spell.

Okay, that was very abstract, now let me get a bit more into detail:

This should be done during map initialization, so use the init function in your Header library, create the cast in it and save it in a global variable:
library myTower initializer init globals Cast myCast endglobals //The init function private function init takes nothing returns nothing set myCast = Cast.create(...) endfunction endlibrary
This was how to do it. But what values have to be handed to the .create method of Cast?:
static method create takes integer abil, string order, real lifetime returns Cast
So, as you see, you need the code of an ability "abil", that is the ability that should be cast. Then you need an order to cast it, this is the orderId string for the ability, for example "chainlightning" for a chainlightning. The last parameter "lifetime" is the time the dummy will live. You should set this time to how long the spell takes. So if it is a damage over time that lasts 15 seconds, "lifetime" must be at least 15 seconds!

Okay, now we have initialized our cast. But now we want to cast it. For example we want to cast it whenever the tower attacks a creep. So we use onAttack:
function onAttack takes Tower tower returns nothing

Here we have to add the dummy cast, there are many methods for doing that:

//== immediate casts == method immediateCastFromCaster takes Unit castingUnit, real dmgRatio, real critRatio returns nothing method immediateCastFromUnit takes Unit castingUnit, Unit target, real dmgRatio, real critRatio returns nothing method immediateCastFromPoint takes Unit castingUnit, real x, real y, real dmgRatio, real critRatio returns nothing //== unit targeted casts == method targetCastFromCaster takes Unit castingUnit, Unit target, real dmgRatio, real critRatio returns nothing method targetCastFromTarget takes Unit castingUnit, Unit target, real dmgRatio, real critRatio returns nothing method targetCastFromPoint takes Unit castingUnit, Unit target, real x, real y, real dmgRatio, real critRatio returns nothing //== point targeted casts == method pointCastFromTargetOnTarget takes Unit castingUnit, Unit target, real dmgRatio, real critRatio returns nothing method pointCastFromCasterOnTarget takes Unit castingUnit, Unit target, real dmgRatio, real critRatio returns nothing method pointCastFromPointOnTarget takes Unit castingUnit, real fromX, real fromY, Unit target, real dmgRatio, real critRatio returns nothing method pointCastFromUnitOnCaster takes Unit castingUnit, Unit from, real dmgRatio, real critRatio returns nothing method pointCastFromCasterOnCaster takes Unit castingUnit, real dmgRatio, real critRatio returns nothing method pointCastFromPointOnCaster takes Unit castingUnit, real fromX, real fromY, real dmgRatio, real critRatio returns nothing method pointCastFromUnitOnPoint takes Unit castingUnit,Unit from, real toX, real toY, real dmgRatio, real critRatio returns nothing method pointCastFromCasterOnPoint takes Unit castingUnit, real toX, real toY, real dmgRatio, real critRatio returns nothing method pointCastFromPointOnPoint takes Unit castingUnit, real fromX, real fromY, real toX, real toY, real dmgRatio, real critRatio returns nothing //== no casts == method noCastFromCaster takes Unit castingUnit, real dmgRatio, real critRatio returns SpellDummy method noCastFromUnit takes Unit castingUnit, Unit from, real dmgRatio, real critRatio returns SpellDummy method noCastFromPoint takes Unit castingUnit, real fromX, real fromY, real dmgRatio, real critRatio returns SpellDummy method castOnAOE takes Unit castingUnit, real x, real y, real radius, real dmgRatio, real critRatio, integer targType returns nothing
Getting a system into these function is very simple. The name is assembled like this: [CASTTYPE]CastFrom[FROMTYPE]On[TARGETLOC] CASTTYPE is the type of the order the dummy gets -> immediate: An immediate cast like warstomp -> target: A unit targeted spell like thunderbolt -> point: A point / location targeted cast like rain of fire FROMTYPE represents from where the spell is cast: -> Caster: The spell is cast from the caster's position -> Unit: The spell is cast from another unit (which has to be specified) -> Target: The spell is casted from the target's position -> Point: The spell is casted from a point which has to be specified (x,y) TARGETLOC is only used by point cast spells and specifies which point to target: -> Caster: The spell is cast onto the caster's position -> Target: The spell is casted onto a target's position (you must specify the targ) -> Point: The spell is casted onto a point which has to be specified (x,y)

So basically you have to answer two questions to choose the correct method:

  • What kind of spell am I casting? (Immediate, Unit Targeted, Ground Targeted)
  • From what position should the spell be cast? (The caster's position? The target's position? Any other position?)
  • Only for ground targeted spells: Onto which point should be cast? (Onto a target? Onto the caster? Onto another position?)
The noCast methods just create and return a dummy without casting anything. This is useful for giving the dummy passive abilities afterwards like an aura. The castOnAOE method casts a unit targeted spell onto all units in a range around a target point.

dmgRatio is just a real percentage value of how much damage this spell should deal. So, if it should deal as much as stated in the object editor, set it to 1.0. If you set it to 3.0 for example, the spell will do 300% of the damage in the object editor. This is an easy way to enhance spells for example based on the level of the tower, for each level, the tower could get +10% spell damage without having to create an ability with many levels.

critRatio is a function from the crit System. To get a critRatio, use the tower method:

method calcSpellCrit takes real bonusChance, real bonusDamage returns real
This function calculates all crit modifiers the tower has from items and buffs and returns a value that can be inserted as critRatio. The two parameters bonusChance and bonusDamage state values to alter the crit chance of the tower and the crit damage. Basic spell crit damage is 125-175% (depending on the tower's level). So if you insert 0.5 = 50% as bonusDamage the tower will do 120% + 50% = 170% damage upon critting (for a level 0 tower). bonusChance is a value to increase the crit chance of the tower. The basic crit chance is 1.25%. But this chance is raised by items, buffs and can also be raised for certain spells by inserting a number greater than 0 into bonusChance. If you insert 1, the tower will always crit with this spell, if you insert 0.5 there will be a 50% increased chance for critting.

Okay, so lets say, our spell is unit targeted, should do as much spell damage as stated in the object editor (dmgRatio = 1) and should have a 50% chance to hit critical with no extra bonus damage, so the call would look like this:
function onAttack takes Tower tower returns nothing call myCast.targetCastFromCaster(tower,Event.getTarget(),1.0,tower.calcCrit(0.5,0.0)) endfunction
That's it! Remember from the first chapter that we can use Event.getTarget() to get the creep that is attacked. I apologize for the complicated system with dmgRatio and critRatio. But that was the only way to implement an efficient spell damage crit and amplifying system. I know that these values, paired with the calcCrit function will take you some extra time to learn. But once you have learned it, you will have easy access to let your spells to as much damage as YOU want.

So as a small recap, dummy spells a cast in two stages: Create a cast object upon map init. Then in reaction to an event, cast it!

I didn't want to make you mad in the last chapter, so I didn't tell you everything about the great stuff the API allows you to do with dummy units. So if you are quite familiar with the last chapter and know the three steps it takes to cast a dummy spell, then read on and I show you how deep the rabbit hole goes. If you have already enough for your spells, skip this chapter.

You can give your cast a custom damage table. A damage table alters the dummy's damage against specific categories or sizes. So, for example you can make a chainlightning that deals +100% damage to undead units. You do this right after creating your cast (at map init).

The method to do so is:

method setCustomDamageTable takes DamageTable table returns nothing
but it needs a damage table. We can create an "empty damage table" i.e. a table that doesn't modify anything yet by invoking the constructor:
local DamageTable myTable = DamageTable.create()
Now we want to set the values for different sizes/categories for this table like the +100% to undead. we use these table's methods:
struct DamagaTable //Sets the bonus against a category whichCategory for this damage table to value method setBonusToCategory takes integer whichCategory, real value returns nothing //Sets the bonus against a size whichSize for this damage table to value method setBonusToSize takes integer whichSize, real value returns nothing
The first function sets the damage against a category (human, undead,...), the second one for a size (mass,boss,normal...). They need a modId and a real value. The real value is just the value we want to (de-)amplify the cast, so it would be 1.0 for our +100%. The modId is just one of the creep categories/sizes (You can find them in the Creep library):
//Creep size constants constant integer SIZE_MASS = 0 //Mass creeps (very unhealthy) constant integer SIZE_NORMAL = 1 //Normal creeps (the ones that spawn in normal levels) constant integer SIZE_CHAMPION = 2 //Champions are larger units that spawn with normal creeps constant integer SIZE_BOSS = 3 //Bosses are even mightier than champions (the will probably spawn alone) constant integer SIZE_AIR = 4 //Air creeps //Creep categorization constants constant integer CATEGORY_UNDEAD = 0 //Undead creeps constant integer CATEGORY_MAGIC = 1 //Magical creeps constant integer CATEGORY_NATURE = 2 //Nature creeps constant integer CATEGORY_ORC = 3 //Orc creeps constant integer CATEGORY_HUMANOID = 4 //Humanoid creeps
so for giving +100% to undead we would call:
call myTable.setBonusToCategory(CATEGORY_UNDEAD,1.0)
Always use the constants when invoking such a method, not the raw integer values! Raw integer values will make your code unreadable (and readable code is a big issue if you want your tower in the map!). With the category constant, everybody immediatly sees that you want to alter damage to undead by +100%.

After we have altered the table we can just hand it to the setCustomDamageTable method and every dummy created from this Cast will have this table.

Putting it all together: This would be your init library with the +100% to undead modification:

library myTower initializer init globals Cast myCast endglobals //The init function private function init takes nothing returns nothing local DamageTable myTable set myCast = Cast.create(...) //Creating the cast set myTable = DamageTable.create() //Creating the damage table call myTable.setBonusToCategory(CATEGORY_UNDEAD,1.0) //Altering the table call myCast.setCustomDamageTable(myTable) //Setting the table for the cast endfunction endlibrary

That's it! Quite straight, isn't it? Note one last thing: In addition to this table, ALWAYS also the table of the tower applies to a cast. Their values are added. So if your tower alread deals +100% damage to undead, your spell would now deal +200% damage to undead. If you don't want your tower's modifications in your spell, just "include" the exactly opposite value's in your towers table.

Example: Your tower deals only 50% (-50%) to bosses and 150% (+50%) to undead. Now you want a spell that deals 100% to everything. So you would add +0.5 to bosses and -0.5 to undead to the Cast's table. This would compensate the tower's table.

There are two events you can react to for certain dummies. They are onKill (when the dummy kills a creep) and onDamage (when the dummy damages a creep). The methods for setting them are the following:

function interface DummyEventHandler takes DummyUnit dummy returns nothing struct Cast method setDamageEvent takes DummyEventHandler ev returns nothing method setKillEvent takes DummyEventHandler ev returns nothing

So you need to create a function that satisfies the interface DummyEventHandler. Then you can set this function as damage or kill event for your Cast.

Okay, for those who are not familiar with vJASS function interfaces a short explanation of what you need to know: As you see, the two methods take a variable of type "DummyEventHandler". This type is declared as a function interface with the signature "takes DummyUnit dummy returns nothing". This just means we have to code a function with EXACTLY this signature (the name "dummy" doesn't matter, but the take and return types and order must be exactly the same!). If we have coded such a function, we can hand it to the method setKillEvent by just writing

<NAME OF THE FUNCTION INTERFACE IT SHOULD SATISIFY>.<NAME OF OUR FUNCTION>
So if our written function was named myFunction, you would write
call myCast.setKillEvent(DummyEventHandler.myFunction)
to entry the function as kill event.

An example: Let's create a small example that displays a debug message whenever our dummy kills a creep.

First we create an event reaction function with the signature of DummyEventHandler (in our myTower library):

function myKillEvent takes DummyUnit d returns nothing call BJDebugMessage("The dummy has killed a creep!") endfunction
As you see, the function MUST take a DummyUnit to satisfy the function interface (Even if we don't need it here, we just simply display a debug message as event reaction).

The next and already final step is to add this function as kill event for our cast by invoking the setKillEventMethod. This is how our myTower library would look like now:

library myTower initializer init globals Cast myCast endglobals //Our kill event reaction function function myKillEvent takes DummyUnit d returns nothing call BJDebugMessage("The dummy has killed a creep!") endfunction //The init function private function init takes nothing returns nothing local DamageTable myTable set myCast = Cast.create(...) //Creating the cast call myCast.setKillEvent(DummyEventHandler.myKillEvent) //Setting the kill event reaction function endfunction endlibrary

That's it! And believe me this is a mighty tool! Examples: A chainlightning that stuns everything it hits: Just code a method that casts a dummy stun on the victim and set it as "setDamageEvent". A fireball that bounces to the nearest creep if it kills a creep: Write a function that finds the nearest target and casts a fireball on it and set this function as "setKillEvent" for your fireball. Since the casted spell and the spell it is cast from are the same this is automatically recursive: Whenever the new spawned fireball kills a unit it will again jump to the next target. You could also make a chain reaction by spawning two fireballs for each fireball that kills a creep. Phat stuff, isn't it?

To get the damaged / killed creeps, use Event.getTarget() (like for all event reactions). For the onDamage event, you can get and set the event damage with Event.damage. So you could make a spell that gives its tower one mana for each 10 damage point it deals (for example). Just add Event.damage/10 to the tower's mana as an event reaction to the dummy damage event.

The buff system allows you to create triggered buffs that modify the creep/tower values and can react to events like the buffed unit dies, the buff expires and so on. This system is very mighty, it allows you hundred times more stuff than you could do with normal untriggered buffs.

Just like each dummy belongs to a type that specifies its values, which is represented by the struct Cast, each buff belongs to a type that represents its values, represented by the struct BuffType. To create a new kind of buff, you create a BuffType in your init function, save it in a global variable and then you can apply it to units.

The create method of BuffType looks like this:

static method create takes real timeBase, real timeLevelAdd, boolean friendly returns BuffType
The values have the following meaning:
  • timeBase: The time in seconds a buff last on a unit, if its power level is 0. If this value is set to -1, then the buff is permanent.
  • timeLevelAdd: The time in seconds that is added to the base time for each power level a buff has. So for example if this is 10, then a power level 3 buff will last 30 seconds longer than a level 0 buff. If the buff is permanent, you can just set this to zero (or any other value).
  • friendly: If this buff is a positive (true) or negative (false) buff.
The "power level" of a buff is a measure for how strong it is. We will talk about that later. If you set timeBase to -1.0 then the buff will last infinitly on its target. This can be used to create permanent buffs or buffs that are removed when some conditions are true, like when the unit attacks or something like that. After invoking this method, we have a BuffType with a specific duration. However the buff doesn't do anything yet.

The easiest way to let our buff do something is setting its modifier. A modifier set this way modifies the unit values of the buff as long as the unit has the buff on it. As soon as the buff is removed, the modifications will vanish from the unit. To set the modifier of a buff type use these methods:

method setModifier takes Modifier mod returns nothing method setBuffModifier takes Modifier mod returns nothing
this will set the buff type's modifier. Whenever a buff of this type is applied to a unit, all modifications stated in the modifier will be applied. The difference between this two functions is this: If setModifier is used the modifier gets stronger according to the TOWER LEVEL. If setBuffModifier is used the modifier gets stronger according to the BUFF POWER LEVEL.

Example how to create a buff type and add some modifications to it: (In the header trigger)

library myTower initializer init globals BuffType myType endglobals //The init function private function init takes nothing returns nothing local Modifier mod = Modifier.create() //Create the modifier set myType = BuffType.create(10.0,1.0,true) //Create the bufftype call myType.setBuffModifier(mod) //Set the modifier for the bufftype call mod.addModification(MOD_DMG_TO_ORC,0.2,0.05) call mod.addModification(MOD_MANA,100,10) endfunction endlibrary
This creates a buffType that lasts 10 seconds + 1 sec/power Level on a unit, it is a positive buff (true). It adds 20% damage against orc creeps on power level 0 and additional 5% for each power level. It also adds 100 to the tower's mana and +10 mana for each power level.

Okay, now you have created a buff type. So lets use it!

You can apply a buff onto a unit by just invoking this method of BuffType:

method apply takes Unit caster, Unit target, integer level returns Buff
It takes two Units, the unit that casted the buff (caster) and the unit it was cast on (target). As a third parameter it takes the power level the buff should have (level).

Just call this function whenever you want to place a buff. The engine does the rest (i.e. checking if the unit already has the buff, doing the modifications, removing it when its time is up and so on).

As already stated, the buff's power level is used for the following things:

  • It modifies the time, the buff will last on the unit
  • It modifies the strength of its modifier (if it has one)
  • It is used for stacking purposes. A power level 5 buff will overwrite a power level 3 buff, but not vice versa.
  • Example: Lets take the buff type we have created in the example above. Lets make that our tower has a 10% chance on each attack, that the tower will buff itself with the buff. As buff power level, the tower will use its own level. So we enable the onAttack trigger and alter it this way:

    globals constant real ONATTACK_chance = 0.1 //10% chance to cast the buff constant real ONATTACK_chanceLevelAdd = 0.0 //No additional chance per tower level endglobals function onAttack takes Tower tower returns nothing call myType.apply(tower,tower,tower.getLevel()) //Apply the buff onto the tower, casted by the tower itself endfunction
    One line of code. Easy, isn't it?

    When you create a buff type, I showed you how to add modifications to it. However, modifications alone are not too flexible. Event reactions are much more powerful. A buff (or the unit it is cast on) produces a large number of events, that we can catch by setting event reaction handlers. We can react to the following events:

  • On Create: This event is fired whenever the buff is created (i.e. applied to a unit that doesn't have it yet).
  • On Refresh: This event is fired whenever the buff is refreshed (i.e. applied to a unit that already has it with the same level).
  • On Upgrade: This event is fired whenever the buff is upgraded (i.e. applied to a unit that already has it with a lower level).
  • On Cleanup: This event is fired when the buff is somehow removed or the unit it is on dies. Do your garbage collection tasks here.
  • On Purge: This event is fired when the buff is forcibly removed by a purge like ability.
  • On Expire: This event is fired when the buff is removed due to having used up its duration.
  • Periodic: This event is fired periodically (you can specify the period).
  • On Spell Cast: This event is fired whenever the buffed unit casts a spell
  • On Spell Target: This event is fired whenever the buffed unit is targeted by a spell
  • On Death: This event is fired when the buffed unit dies
  • On Kill: This event is fired whenever the buffed unit kills a unit
  • On Attack: This event is fired whenever the buffed unit attacks
  • On Attacked: This event is fired whenever the buffed unit is attacked
  • On Damage: This event is fired whenever the buffed unit damages a creep
  • On Damaged: This event is fired whenever the buffed unit is damaged
  • On Unit Comes In Range: This event is fired whenever a unit (which can be filtered like in the tower event) comes in a specific range to the buffed unit.
  • Ability: No real event reaction but something else. The unit will have an ability as long as the buff is on it. You can specify arbitrary abilCodes here.
  • Autocast: This is no real event reaction. We can even add an autocast to the buff, so the unit will be granted an active ability that is also automatically cast, as long as it has the buff. However, this is very exotic and shouldn't be used often and won't be covered here.
  • Aura: We can also add an aura to this buff. So the buffed unit will have an aura as long as it has this buff. This is also very exotic, so it won't be covered here, too.
So as you can see, you can react to almost everything. This gives you limitless possibilities. Note that not all events make sense for all kinds of units. For example, a tower will never be damaged or attacked! Now I will tell you how this works in code. To add an event reaction handler, first write the reaction handler function. It should (must) have this signature:
function NAME takes Buff PARAMNAME returns nothing
NAME and PARAMNAME can be set arbitrarily (as names don't matter for function interfaces). After you have written such a function you can call the appropriate setEvent or addEvent method of the BuffType to set this function as an event handler. (Note that most of these functions are not defined in the struct BuffType but in the struct EventTypeList which is a supertype of the struct BuffType. Whenever the method's starts with "add" instead of "set", it shows that you can add more than one of those event handlers to it. However, more than one event handler for one event is not very useful. Here they are:
struct EventTypeList //Sets the event handler that is called when the event placer is somehow destroyed (expired or removed or its target dies...) method setEventOnCleanup takes EventHandler be returns nothing //Sets the modifier of this event placer method setModifier takes Modifier mod returns nothing //Sets the event handler that is called when this EventTypeList is applied onto a unit method addEventOnCreate takes EventHandler be returns nothing //Adds an event handler that is called every seconds (period can not be smaller than 0.2) method addPeriodicEvent takes EventHandler be, real period returns nothing //Adds an event handler that is called when the buffed unit casts a spell method addEventOnSpellCast takes EventHandler be returns nothing //Adds an event handler that is called when the buffed unit is targeted by a spell method addEventOnSpellTarget takes EventHandler be returns nothing //Adds an event handler that is called when the buffed unit dies //or if it is destroyed / upgraded method addEventOnDeath takes EventHandler be returns nothing //Adds an event handler that is called when the buffed unit kills a unit method addEventOnKill takes EventHandler be returns nothing //Adds an event handler that is called when the buffed unit gains a level method addEventOnLevelUp takes EventHandler be returns nothing //Adds an event handler that is called with chance% when the buff's target attacks //chanceLevelAdd increases the chance for each level the unit has, //so if it is set to 0.04 for example, a level 10 unit will have a 40% (0.04*10=0.4) higher chance than a level 0 unit method addEventOnAttack takes EventHandler be, real chance, real chanceLevelAdd returns nothing //Adds an event handler that is called with chance% when the buffed unit is attacked //chanceLevelAdd increases the chance for each level the unit has, //so if it is set to 0.04 for example, a level 10 unit will have a 40% (0.04*10=0.4) higher chance than a level 0 unit method addEventOnAttacked takes EventHandler be, real chance, real chanceLevelAdd returns nothing //Adds an event handler that is called with chance% when the buffed unit deals damage //chanceLevelAdd increases the chance for each level the unit has, //so if it is set to 0.04 for example, a level 10 unit will have a 40% (0.04*10=0.4) higher chance than a level 0 unit method addEventOnDamage takes EventHandler be, real chance, real chanceLevelAdd returns nothing //Adds an event handler that is called with chance% when the buffed unit is damaged //chanceLevelAdd increases the chance for each level the unit has, //so if it is set to 0.04 for example, a level 10 unit will have a 40% (0.04*10=0.4) higher chance than a level 0 unit method addEventOnDamaged takes EventHandler be, real chance, real chanceLevelAdd returns nothing //Adds an event handler that is called when a unit which passes the target type test against targType // comes in range range of the buffed unit. method addEventOnUnitComesInRange takes EventHandler be, real range, integer targType returns nothing endmethod //Adds an ability placer. The ability abilId will be added automatically //to the unit as long as is it has the event placer //If safeStacking is set to true, then the unit will keep a reference count //for this ability to check how many event placers place it on this unit //So if two placers place it and one is removed the ability is still on the unit //If safeStacking is false, then the ability will be removed upon removing the //first placer, even if there is another one that gives it. //Enable safeStacking if you know that the ability can be granted more than once //Disable it if you are sure that this event placer (and any other that grants that ability) //is never twice on a unit method addAbility takes integer abilId, boolean safeStacking returns nothing endmethod //Adds an aura of type auraType, for more infos check the struct AuraType method addAura takes AuraType auraType returns nothing struct BuffType extends EventTypeList //Sets the event function that is called when this buff is cast on a unit that already has it (with the same level) method setEventOnRefresh takes EventHandler be returns nothing //Sets the event function that is called when this buff is upgraded (i.e. cast on a unit that has it with a lower level) method setEventOnUpgrade takes EventHandler be returns nothing //Sets the event function that is called when the buff runs out of lifetime (just before it is removed) method setEventOnExpire takes EventHandler be returns nothing //Sets the event function that is called when the buff is forcibly removed from the unit (by a purge like ability for example) method setEventOnPurge takes EventHandler be returns nothing //Sets the buff modifier of this buff //The buff modifier is a modifier (just like when using a normal addModifier on EventPlacer (the parent type of this type)) //The difference is that this modifier is only modified by the BUFF LEVEL, not the unit's level method setBuffModifier takes Modifier m returns nothing

In the event reaction function itself, you can use many methods of the buff. For example getBuffedUnit() will get the unit container, on which the buff is cast.

An example will make this more clear and will show how mighty this concept is. Let's create a buff, that whenever the buffed tower attacks, there is a 10% chance that the tower will deal 100 bonus spell damage to the attacked creep. First the reaction function:

function myEventHandler takes Buff b returns nothing call b.getBuffedUnit().doSpellDamage(Event.getTarget(),100,t.calcCrit()) endfunction
Again we use Event.getTarget() to refer to the attacked unit. This is the function. Now just tell the bufftype to call this function in 10% of all attacks. We do this in the init function after creating our buff type:
private function init takes nothing returns nothing set myType = BuffType.create(10.0,1.0,true) //Create the bufftype call myType.addEventOnAttack(EventHandler.myEventHandler,0.1,0.0) //Set our event handler with a 10% chance endfunction

The whole code would be:

library myTower initializer init globals BuffType myType endglobals function myEventHandler takes Buff b returns nothing call b.getBuffedUnit().doSpellDamage(Event.getTarget(),100,t.calcCrit()) endfunction private function init takes nothing returns nothing set myType = BuffType.create(10.0,1.0,true) //Create the bufftype call myType.addEventOnAttack(EventHandler.myEventHandler,0.1,0.0) //Set our event handler with a 10% chance endfunction endlibrary

As you saw in this example we used .getBuffedUnit() on the buff to get the unit it buffs, there are some more useful methods for buffs, some are lended from its super type EventPlacer (Again, this list is incomplete, for a full list, check the API in the content creation map):

struct EventPlacer //Gets the Unit that is buffed by this eventPlacer method getBuffedUnit takes nothing returns Unit struct Buff extends EventPlacer //Gets the power of this buff (for normal buffs, this is equal to the level) method getPower takes nothing returns integer //Gets the Unit that casted this buff method getCaster takes nothing returns Unit //Returns how many seconds this buff still has until it expires method getRemainingDuration takes nothing returns real //Removes this buff from the unit method removeBuff takes nothing returns nothing
A very useful function is the removeBuff function. It removes the buff immediately. Another one is getCaster which gets you the Unit that casted the buff.

Maybe you want your buff to have a buff icon with description as normal WC3 buffs have or maybe an effect on its target. That is no problem. Just create a buff in the object editor (here you can also choose an effect). Then, you can set it as a BuffType's WC3 buff by using this method:
struct BuffType //Sets the buff icon for this buff, i.e. the buff that will //be displayed on the unit. Enter a valid buff id for your buff here like 'B000' method setBuffIcon takes integer buffId returns nothing
Example, lets assume you created an Object Editor buff with the id 'B000' and myBuffType is the buff type you want to add that buff to:
call myBuffType.setBuffIcon('B000')
That's all! Now the buff will have a buff icon/effect as long as it is on the unit.

Here I will tell you some more things you can do with buffs. If you feel safe with buff handling, read on here.

Until now, each buff stacks with each other buff except for two buffs of the really same buff type. However, you might want to design a whole family of towers that apply the same buffs, but these buffs shouldn't stack, only the strongest one should be on a unit at one time. Another example would be slows. There are a few major slow categories in the game, like ice slow, poison slow, heat slow,.... Many towers of the element "Ice/Water/However it will be named in the final version of YouTD" will have slows. If all of these slows would stack, this element could slow all creeps to minimum speed without a big effort. So most of the buffs of this element should share the same "stacking group", i.e. none of them should stack with each other, only the strongest one will be on a unit.

A stacking group in YouTD is just a string. All buffs that have the same string will not stack with each other. If you want your BuffType to be in a stacking group, you first have to find its name. If it is your own stacking group, that is only for your family of towers, you can give it an arbitrary name, but try to give it one that will not collide with other stacking groups. Including your nick for example in the group's name will make collisions very unlikely. If the stacking group is a global one like the ice / poison / heat slow, then you will find its name in a list on the YouTD homepage where all major global stacking groups will be released.

If you know the name of the group, just call the buff types method setStackingGroup which takes the string as its only parameter. Example:

call myType.setStackingGroup("gexStackingGroup")
will set the stacking group of the buff type myType to "gexStackingGroup". If other buff types share this group, they won't stack with this buff type.

There are some problems that arise from two different buffs not stacking with each other. The first problem: If one buff is on the unit and another buff of the same level wants to be applied to the unit, should the old buff replaced or be kept?

So for buffs that belong to a stacking group, other rules apply: -If a buff of a stacking group tries to apply to a unit that has a buff of the same level, the old buff will be kept but NOT refreshed. The new buff will just be rejected. -If a buff of a stacking group tries to apply to a unit that has a buff of a lower level, then the old buff will be removed (so cleanup events will fire on it) and then the new buff will be applied (which fires on creation events for it). No refresh or upgrade events will be fired.

The other problem with stacking groups is that for stacking groups level and power level are two different things. The power level of a buff tells, how its modifications and its duration are amplified. The level of a buff compares it with other buffs and decides if a new incoming buff is refreshed, upgraded or declined.

For normal buffs these two values were the same, since a single buff is always stronger if is modifications are more intense and vice versa. For stacking groups this is not true anymore. Imagine one buff, lets call it A, that has +100% damage base and +1% damage per power level. Another buff, called B, of the same stacking group has +10% damage and +10% damage per power level. Imagine a unit has Buff B with a power level of 5 on it. so it does +60% damage. Then Buff A with power level 0 is applied. Buff A has a lower power level, but has better values (100% damage versus 60%). So if only the power level was used for stacking, A wouldn't overwrite B, but everybody would agree that A should overwrite B. So we have level and power level for stacking group buffs. To differ between them, use the advanced apply function instead of the normal one:

method applyCustomPower takes Unit caster, Unit target, integer level, integer power returns Buff
in this function, you can state a level and a power value. The power value will alter the buff's modifications and the duration, the level will do the stacking stuff.

How to set the level for such a buff? For your own private stacking groups, you will have no problem in finding apropriate level values. For global stacking groups a metric must be found to map buff strength to level. You will find such metrices on the page where the global stacking groups are defined.

You already saw, that when using stacking groups, you should use the function "applyCustomPower", not only "apply". There are two other advanced apply functions that let you specify explicitly the time the buff will last:
//Applies a buff of this type, cast by caster onto the target target with level level //If the target doesn't have this buff already, then it is created (onCreate event is fired) //If the unit already has this buff, there are three possibilities //--> The unit's buff has a higher level than this one -> nothing is done //--> The unit's buff has the same level than this one -> the buff duration is refreshed and an onRefresh event is fired //--> The unit's buff has a lower level than this one -> the buff duration is refreshed and an onUpgrade event is fired method apply takes Unit caster, Unit target, integer level returns Buff //Applies a buff with custom time (rest is the same as in normal apply) method applyCustomTimed takes Unit caster, Unit target, integer level, real time returns Buff
The difference is that in the upper one, power and level are divided, so the upper one should be used for stacking group buffs.

If you use these, your buff will exactly last "time" seconds on the unit. So the values you stated in the BuffType's constructor will not matter anymore. You might want this to add buffs that last a random period of time on units or which's duration is altered by some other things.

You can use these two methods of Unit to check/get the buff a unit has of a specific type/stacking group:

//Returns the buff of BuffType that the unit has //If the unit has no buff of this buff type, 0 will be returned //So you can use uc.getBuffOfType(...) != 0 to check if a unit container has a specific buff method getBuffOfType takes BuffType bt returns Buff //Returns the buff of the stacking group named that the unit has //If the unit has no buff of this stacking group, 0 will be returned //So you can use uc.getBuffOfGroup(...) != 0 to check if a unit container has a buff of //a specific stacking group method getBuffOfGroup takes string s returns Buff
You could use these for example for a tower that has a chance to place a buff and then on each attack deals bonus damage if the creep has the buff.

You might want to create an ability that removes positive/negative buffs from units. For this, this method can be used:
//Forces to remove a buff (the first in the buff list) //positive: If set to true a positive buff is removed, else a negative one //This method returns true if there was a buff to remove, otherwise false //Buffs removed by this method will trigger a purge buff event before beeing destroyed method purgeBuff takes boolean positive returns boolean
It removes either a positive or a negative buff and fires the buff's onPurge events. It will return true if there was a buff to remove and false otherwise.

Projectiles are moving visble dummy units, that can move in many ways (home at a target, fly straight ahead, fly balistically impacting or bouncing from the ground and some more). By default, they don't do anything but moving. However, just like with buffs, you can react to many events, thus giving them influence on the gameplay.

Just like for dummies and buffs. Projectiles are created in two steps: First creating a ProjectileType on map init and then launching projectiles of this type.

You create a new projectile type like this:

struct ProjectileType static method create takes string model, real lifetime, real speed returns ProjectileType
It takes the following values:
  • model: The path of the model that should be used like: "Abilities\\Weapons\\Mortar\\MortarMissile.mdl" (remember to escape the backslashes with another backslash)
  • lifetime: The time in seconds this projectile lives until it is removed.
  • speed: The starting speed of the projectile, measured in length units per second. (The same measure as object editor speed values)

Now you have a projectile type with a model, lifetime and speed. Let's launch some of these projectiles!

After you have created a ProjectileType, you can launch projectiles from it. You do this by invoking one of the constructors of the struct Projectile:

struct Projectile extends DummyUnit static method create takes ProjectileType pt, Unit caster, real x, real y, real facing, real damageRatio, real critRatio returns Projectile static method createFromCaster takes ProjectileType pt, Unit caster, real facing, real damageRatio, real critRatio returns Projectile static method createFromUnitToPoint takes ProjectileType pt, Unit caster, Unit from, real toX, real toY, real damageRatio, real critRatio returns Projectile static method createFromPointToUnit takes ProjectileType pt, Unit caster, real fromX, real fromY, Unit to, real damageRatio, real critRatio returns Projectile static method createFromUnitToUnit takes ProjectileType pt, Unit caster, Unit from, Unit to, real damageRatio, real critRatio returns Projectile
Everyone of them takes these values:
  • pt: The projectile type to which this projectile belongs
  • caster: The unit that casted the projectile
  • dmgRatio, critRatio: The same as for dummy casts. Check the chapter Damage- and Critratio for more information.
The different constructors allow you to simply create projectiles from a location to another one. The first one creates the projectile at x,y facing a specific direction. The next one creates a projectile from the position of the caster facing a specific direction. The other ones don't let the projectile face a direction. Instead, you specify a target location or unit. The facing of the projectile will be calculated implicitly so that it will hit the target spot.

Example: Let's create a tower that shoots a projectile into the direction of the attacked creep everytime it attacks. First we create the projectile type in our header:

library myTower initializer init uses libHeader globals ProjectileType myPT endglobals private function init takes nothing returns nothing set myPT = ProjectileType.create("Abilities\\Weapons\\Mortar\\MortarMissile.mdl",4.0,500.0) endfunction endlibrary
The projectile uses the mortar missle model, and flies with 500 speed for 4.0 seconds. Now, launch the projectile upon attack (In the "On Attack" trigger):
globals constant real ONATTACK_chance = 1.0 //Launch on every attack constant real ONATTACK_chanceLevelAdd = 0.0 endglobals function onAttack takes Tower tower returns nothing call Projectile.createFromUnitToUnit(myPT,tower,tower,Event.getTarget(),1.0,tower.calcSpellCrit()) endfunction
We create the projectile using the "createFromUnitToUnit" method. The projectile is casted and spawned by the tower, so we use tower twice. Next, we use Event.getTarget() to get the attacked creep. For the critRatio we use the tower.calcSpellCrit() method. However, if the projectile deals no damage, we can also put an arbitrary number here.

This example created a simple projectile. However, that projectile doesn't do anything yet. So read on to give your projectile more possibilities.

You can set two basic events for a projectile: onExpiration and onCleanup. OnCleanup is fired when the projectile is somehow destroyed. OnExpiration is only fired if the projectile is destroyed due to having used up its lifetime. You can react to them by first writing an event reaction handler which has to use this function interface:
//A function that is used to react on untargeted projectile events function interface ProjectileEvent takes Projectile p returns nothing
After writing this function you can pass it to one of the methods of your projectile to set it as an event handler:
struct Projectile extends DummyUnit //Sets the event on cleanup, //cleanup is executed whenever the projectile is destroyed, no matter how it was destroyed //Do your cleanup and garbage collection tasks here method setEventOnCleanup takes ProjectileEvent e returns nothing //Sets the event that is executed when the projectile reaches its maximum lifetime method setEventOnExpiration takes ProjectileEvent e returns nothing
However, that is not all. Until now, our projectiles fly only straight ahead without colliding with anything. We can change that enabling different modes:

There are some "modes" you can enable for your projectile type. They are:

  • collision mode: If this projectile hits a unit an onCollision event will be triggered.
  • periodic mode: An event is fired periodically for this projectile
  • homing mode: This target homes towards a specific target. When it reaches it an onHit event is triggered and the projectile is destroyed
  • physics mode: This projectile is launched into the air and pulled down to earth by gravity. Upon reaching the ground it may bounce from it many times or impact immediately triggering an onBounce/onImpact event.

You can able one or many of these modes for a projectile type by calling the appropriate methods of ProjectileType:

struct ProjectileType //Enables the collision for this projectile. This means whenever a unit that satisfies the targetType comes into //collRadius range of this projectile collEvent will be invoked. //If destroyOnCollision is true, then the projectile will be destroyed upon collison //If not, then the projectile will move on. The projectile can hit every unit only once! method enableCollision takes ProjectileTargetEvent collEvent, real collRadius, integer targetType, boolean destroyOnCollision returns nothing set .collision = true set .collRadius = collRadius set .collTargetType = targetType set .destroyOnCollision = destroyOnCollision set .eventOnCollision = collEvent endmethod //Enables a periodic events for this projectile //The event function periodEvent will be invoked every period seconds method enablePeriodic takes ProjectileEvent periodEvent, real period returns nothing set .eventPeriod = R2I(period*25.0+0.5) set .eventOnPeriod = periodEvent endmethod //Enables the homing mode for this projectile //This means that the projectile will home towards a target //Upon reaching it, the projectile will be destroyed and e will be invoked //controlValue is the steering value of the projectile //If it is set to 0 then the projectile will turn immediatly to the target //If it is set to a value >0 then, it will only change its direction by controlValue //degrees every timestep, so if controlValue is low it is possible that the projectile //misses the target if the target walks in another direction method enableHoming takes ProjectileTargetEvent e, real controlValue returns nothing set .targeted = true set .eventOnHit = e set .controlValue = controlValue endmethod //Enables physics mode for this projectile. That means the projectile will be pulled to the ground //by gravity. Upon reaching the ground it will either bounce from it or impact, beeing destroyed. //The parameters are defined the following: //eventOnBounce: A function that will be invoked when the projectile hits the ground and has bounces left //eventOnImpact: A function that will be invoked when the projectile hits the ground and has no more bounces //left, the projectile will be destroyed after this function is invoked //zSpeed: The start z-axis speed of the projectile, i.e. how fast it flies into the air at the start //numBounces: How often the projectile bounces from the ground before it finally impacts, if set to 0 //then the projectile will not bounce at all //bounceFactor: A real percentage value that states how much of the z-speed with which the projectile //collides with the ground is transferred to its bouncing z-speed. Examples: //bounceFactor = 0.5 -> the projectile will rise with half the z-speed it has upon collision //bounceFactor = 1.5 -> the projecitlw will rise with 150% of the z-speed it has upon collision, i.e. //it will bounce higher and higher each bounce //bounceFactor = 1.0 -> the projectile will have exactly the same speed after the collision, i.e. //it will fly evenly high after every bounce method enablePhysics takes ProjectileEvent eventOnBounce, ProjectileEvent eventOnImpact, real zSpeed, integer numBounces, real bounceFactor returns nothing
These methods are commented well, so I won't renarrate their usage again. However there are still some small things to say:
  • enableHoming and enableCollision use ProjectileTargetEvents. This is another function interface which needs more parameters:
    //A function that is used to react on targeted projectile events (like onHit, onCollision...) function interface ProjectileTargetEvent takes Projectile p, Unit target returns nothing
    As you can see, these take a second parameter target, which states the target hit by/colliding with the projectile.
  • enableCollision uses an integer targetType. Read more about target types at the chapter Target Types.
  • If you enable homing mode, you have to create the projectile with the constructor "createFromPointToUnit" or "createFromUnitToUnit". All other constructors make no sence because they don't state a target unit (and then it is unclear what the target should be).
  • The struct ProjectileType has this method:
    method enableEventExecute takes nothing returns nothing
    If you call it for a projectile type, its event reaction handlers will be executed instead of evalutated. You must use execute whenever a wait or something similiar is used in your event reaction handlers. Then, call this function for your projectile type. If you use no waits, don't call it, since execute is less performant than evaluate.

Now you have the knowledge to react to events and set modes for projectiles. However, there are some more things you can do:

You can set more values for a ProjectileType than the three parameters of its constructor and the modes. Here are some values you can change:
struct ProjectileType //Sets a custom damage table for these projectiles (check the HowTo for more information on damage tables) method setCustomDamageTable takes DamageTable dTable returns nothing set .dTable = dTable endmethod //The acceleration of the projectile in length units per secondē method setAcceleration takes real acceleration returns nothing set .acceleration = acceleration/25.0 endmethod //The rotation of the projectile, i.e. the rate at which the projectile //changes its direction (to create projectiles that fly a curve) method setStartRotation takes real rotation returns nothing set .rotation = rotation endmethod //The starting height of the projectile method setStartZ takes real startZ returns nothing set .startZ = startZ endmethod
So you can make accelerating projectiles, projectiles that fly a curve and other stuff. Check the API for a full list of possibilities.

Until now, you have statically created ProjectileTypes and then launched projectiles that used their parameters. It is also possible to change projectile parameters dynamically. For example, you could react to an onBounce event and disable physics mode in the event handler. So the projectile will fly straight after its first bounce. Or you might want to react to an onCollision event and increase the projectiles speed whenever it collides with something. These things can all be done by just invoking the methods / using the members of the struct Projectile. You gain access to the projectile itself since the constructor returns the projectile and the projectile is also handed to the event reaction handler functions you may have set. First you can get and set some of the projectiles values by just using the struct members:

struct Projectile extends DummyUnit //The projectiles position real x real y real z //The projectile's flight direction real direction //The projectile's rotation (to fly curves) real rotation //The projectile's speed real speed //The projectile's acceleration real accelerate //The remaining lifetime of the Projectile in seconds real remainingLifetime
So you can get and set the projectile's position, its speed, acceleration, remaining lifetime and other stuff. In addition, you can disable and reenable modes that you had enabled in the Projectile's type. Attention: You may not call reenableXXXX if the projectile's type hasn't enbabled that kind of mode (because of that the method is called REenable). You may only reenable modes that were enabled in the type and then disabled at some point. Just use one of these functions to disable or reenable them:
struct Projectile extends DummyUnit //Collision mode method disableCollision takes nothing returns nothing method reenableCollision takes real collRadius, boolexpr collExpr returns nothing //Physics mode method disablePhysics takes nothing returns nothing method reenablePhysics takes real zSpeed, integer numBounces, real bounceFactor returns nothing //Periodic mode method disablePeriodic takes nothing returns nothing method reenablePeriodic takes integer period returns nothing //Homing mode method setHomingTarget takes Unit targ returns nothing method setHomingControlValue takes real controlValue returns nothing
Note that you may rechoose some of the mode parameters when enabling the projectile's modes. If you want to disable homing mode, just call .setHomingTarget(0).

There are some more get and set methods that you may use to alter your projectile:

struct Projectile extends DummyUnit //sets the teamcolor of the projectile method setTeamcolor takes playercolor c returns nothing //Sets the scale of the projectile method setScale takes real value returns nothing //Colors the projectile with red green blue and alpha value //These must be between 0 and 255 //a = 0 -> totally invisible projectile, a = 255 -> totally opaque projectile method color takes integer r, integer g , integer b, integer a returns nothing //Adds an ability to the projectile (use only passive abilities, a projectile //cannot cast active abilities) method addAbility takes integer abilId returns nothing //Removes an ability from the projectile method removeAbility takes integer abilId returns nothing
So as you see, you can recolor and rescale your projectile and add/remove abilities to/from it (like phoenixfire or something like that).

Now you have the tools to alter projectiles during their flight.

As you might have seen, the struct Projectile extends the struct DummyUnit. So you can use all methods of DummyUnit also on projectiles. For example, you can set a kill or damage event for them, get the caster or let them do spell damage (for a complete list check the API for DummyUnits):
struct DummyUnit //Gets the casting Unit of this dummy unit method getCaster takes nothing returns Unit //Sets the damage event of this dummy unit //(The handed function will be executed when this dummy damages a unit) method setDamageEvent takes DummyEventHandler ev returns nothing //Sets the kill event of this dummy unit //(The handed function will be executed when this dummy kills a unit) method setKillEvent takes DummyEventHandler ev returns nothing //Lets this dummy do spell damage //Attention: The spell damage is modified by the dummy's crit and dmgRatio, //so if your dummy had 10.0 dmgRatio and did a crit with 2.0 times damage //a call to this function with amount = 10 would do 10*10*2 = 200 damage // //Note that in addition, the damage done by this function is modified //by the spell resistance of the target and the spell damage amplification of the tower //so if the upper 200 damage was cast from a tower with 1.5x damage multiplier //onto a creep with 50% spell damage reduction, it would cause 200*1.5*0.5 = 150 damage method doSpellDamage takes Unit target, real amount returns nothing

Some methods of the API require you to hand an integer called "targetType" or "targType" to it. These target types specify which type of units this method will target (for example for auras, the target type tells which units will receive the aura effect). At the moment, there are the following constants which can be used as target type:

constant integer TARGET_TYPE_CREEPS //Targets creeps constant integer TARGET_TYPE_TOWERS //Targets friendly towers (from all team members) constant integer TARGET_TYPE_PLAYER_TOWERS //Targets player towers (only from the player, not team members)
As the comments already explain these types devide units into three major categories. Creeps, friendly towers and player towers. The difference between friendly and player towers is that friendly towers will target all towers from your team, while player towers only targets your really own towers. There are more target types to come in future api releases if there is a need for others.

You know normal auras from Warcraft3. YouTD allows you to create triggered auras just like Buffs. A triggered aura is basically an ability that applies a buff to every unit that matches some targeting criteria and comes into the range of the aura. Your tower/item content creation map has the following trigger called "Tower / Item Aura":

globals real AURA_auraRange = RANGE integer AURA_targetType = TARGET TYPE boolean AURA_targetSelf = TARGET SELF integer AURA_level = LEVEL integer AURA_levelAdd = LEVEL ADD BuffType AURA_auraEffect = AURA EFFECT endglobals
To give your tower / item a triggered aura. Just fill in the according values for these variables:
  • AURA_auraRange:The range of your aura.
  • AURA_targetType:Insert a target type constant here. You can check which constants are allowed at the chapter Target Types.
  • AURA_targetSelf:A boolean value if the aura also targets the tower itself.
  • AURA_level:The level of the aura buff if the casting tower is level 0
  • AURA_levelAdd:The value that is added to the level of the aura buff for each level the tower has
  • AURA_auraEffect:A bufftype defining the buff that will be applied to every unit the aura targets
So basically, creating an aura is very simple. First create a BuffType and give it the events/modifier that the aura effect should have. Then state it as auraEffect in this trigger. There are some points to know about auras:
  • The power of the aura buff is always the level of the casting tower. (If you forgot what a power level or the normal level for a buff is read this chapter again
  • The level of the aura buff is calculated using the both values "AURA_level" and "AURA_levelAdd". So if you set level to 5 and levelAdd to 2, then a level 10 tower having the aura will apply the aura effect with level 5 + 2 * 10 = 25
  • When creating the buff type for the aura effect use this constructor instead of the normal one:
    struct BuffType static method createAuraEffectType takes boolean friendly returns BuffType
    It takes less parameters than the normal constructor, since the values for duration don't matter for auras (they have no duration, they expire when the unit gets out of the aura range).
  • When the aura buff is removed due to the unit getting out of the aura range, an onExpiration event is triggered.
  • Whenever the aura giving unit changes, an onRefresh event is triggered. This happens, if there are two units giving the aura and the unit moves out of the range of the first one, so the second one will then give the aura effect to it. It also happens if a unit already has the aura effect but then walks into the aura range of another tower that gives the same aura but with a higher level. So, for auras, onRefresh is also fired if the buff level changes! OnUpgrade is never fired for auras. If the two aura buffs are from a stacking group but not from the same buff type, the old one will be removed and the new one will be created if the aura giver changes. An onCleanup and an onCreate event will be triggered, but no onRefresh event (just as for normal stacking group buffs).
  • Aura Effect buffs cannot be purged.
You could also create own other auras and even give them to buffs (this would be a buff that grants an aura to the buffed unit). But since those kind of buffs seem very bizarre and I don't think you will ever want to create such a buff, this is not covered here. If you really need to create a buff that gives an aura to the buffed unit, open a thread in the YouTD support forum.

You can give your tower/item an active ability which will be cast automatically by the tower that has the ability / carries the item. However, you may not create really an active ability in your Object Editor and give it to the tower, instead just fill in the autocast trigger. The active ability will be created by the script. Just like for auras, there is a trigger for creating an autocast in the content creation map, called "Autocast":

globals integer AUTOCAST_autocastType = AUTOCAST TYPE real AUTOCAST_range = RANGE real AUTOCAST_cooldown = COOLDOWN integer AUTOCAST_manacost = MANA COST //Only for buffs (set all three to 0 if this is no buff) integer AUTOCAST_targetType = TARGET TYPE integer AUTOCAST_numBuffsBeforeIdle = NUM BUFFS BEFORE IDLE BuffType AUTOCAST_buffType = BUFF TYPE endglobals function onAutocast takes Tower tower returns nothing endfunction
As you see, it contains an event reaction handling function "onAutocast" and several values you must set for your autocast.

This is just an event handler that will be called whenever your tower uses this autocast. It is called when the tower casts the spell by itself using the autocast engine. However, it is also cast if the player disables autocasts for this tower and casts the ability manually. So this function is basically the effect of your cast, put the actions that your cast does here.

AUTOCAST_range, AUTOCAST_cooldown, AUTOCAST_manacost are self explanatory. These are the manacost the cooldown and the range of the ability. AUTOCAST_autocastType is the type of cast, it can have the following values (which are defined in the API library called "Autocast Types"):
constant integer AC_TYPE_ALWAYS_BUFF constant integer AC_TYPE_OFFENSIVE_BUFF constant integer AC_TYPE_OFFENSIVE_TARGET constant integer AC_TYPE_OFFENSIVE_IMMEDIATE constant integer AC_TYPE_OFFENSIVE_POINT
The last three are offensive spells. The tower will cast them whenever it attacks a creep and that creep is in their range, the spell is not in cooldown and the tower has enough mana. Just like for dummy spells you can have unit targeted spells (AC_TYPE_OFFENSIVE_TARGET), ground targeted spells (AC_TYPE_OFFENSIVE_POINT) and spells without a target (AC_TYPE_OFFENSIVE_IMMEDIATE). Unit targeted spells will target the attacked unit, ground targeted spells will target the position of the attacked unit. The upper two autocast types are the buff types. Buff autocast types use a special tower AI: The tower will try to buff units in its range. However, it will NOT buff units that already have the buff on them. Only if the buff expires it will try to recast it on them. The difference between the two types, is that AC_TYPE_ALWAYS_BUFF tries to buff units around it whenever it has enough mana. AC_TYPE_OFFENSIVE_BUFF tries to buff units only as long as the tower is attacking (because it is no use to buff certain buffs as long as the towers are not in combat). You should always prefer AC_TYPE_OFFENSIVE_BUFF. Use AC_TYPE_ALWAYS_BUFF only if you really want your tower to buff all the time, even if there are no creeps around.

If you choose one of the two BUFF autocast types, you have to fill in the last three values (If you don't use a BUFF autocast type, just set the three values to 0):

  • AUTOCAST_targetType is the target type of the buff (like explained in the chapter about target types). Use TARGET_TYPE_CREEPS if this is a debuff for creeps and one of the other target types if it is a positive buff for towers.
  • AUTOCAST_numBuffsBeforeIdle states, how often the tower tries to buff units around it before it rechecks if it still in combat. For AC_TYPE_ALWAYS_BUFF autocast types, this value doesn't matter since the tower doesn't check if it is in combat. For AC_TYPE_OFFENSIVE_BUFF however, this value should be set appropriately. A good value is 1, so the tower checks after each successful buff if it is still in combat. If your buff spell has low cooldown and can be cast many times in a few seconds, you should set this value higher that the tower will buff some units before it rechecks if it is in combat (since this rechecking costs time, thus making the tower stop between casts for a small period of time).
  • AUTOCAST_buffType is the buff type of the buff, that this autocast will apply onto units. You must enter your used BuffType here for two reasons: First, the tower uses this buff type to check if a unit already has the buff. If it has, it will target another unit instead. Second, if you leave the event handler "onAutocast" totally empty, but state a buff type here, then the autocast will automatically apply this buff onto the targeted unit using the towers level as buff power and buff level. If the only thing your autocast does is applying the buff onto the target, then do it like this. Leave the handler empty. The enigne will apply the buff for you (which is a bit more performant than calling the autocast handler which then applies the buff). However, if your spell does something else in addition to casting this buff, you have to use the autocast handler and apply the buff yourself. If the autocasthandler is not completely emtpy, the engine won't apply the buff for you. But even if you apply the buff by yourself, state its buff type here, so the engine will do the check if the target already has the buff. If you want to cast your buff by yourself and want that the tower also casts it onto units that already have it, then set AUTOCAST_buffType to 0, then the engine will not check if the unit already has the buff.

As you know, for every event reaction trigger you may set @visible to true in its trigger comment and fill in the values there to create a dummy tooltip ability for this event. For autocasts, there is no @visible flag in the trigger comment, because you MUST ALWAYS entry values here. The reason is clear: An autocast is an ability. This ability always needs an icon and a tooltip. So insert these values here. Then, the autocast ability will have this tooltip and icon. Note that you don't have to include the manacost, range or cooldown into your explain tooltip. They will be added by the script.

The autocast trigger comment looks a bit different than other ones, it has the following parameters:

@icon= @name= @short_explain= @long_explain= @caster_art= @target_art=
The first four parameters are in every trigger comment. However, @caster_art and @target_art are new. Here, you can set model paths of effects that you want your spell to create on its caster / target. Since an active spell should have a cool effect, set appropriate values here. The trigger comment could then look like this for a buff:
@icon=ReplaceableTextures\CommandButtons\BTNInnerFire.blp @name=Example buff @short_explain=Buffs towers around it increasing their damage. @long_explain=Buffs towers around it increasing their damage by 100%. The buff lasts 10 seconds. @caster_art=Abilities\Spells\Human\Heal\HealTarget.mdl @target_art=
This would add an effect with model "Abilities\Spells\Human\Heal\HealTarget.mdl" onto the caster. The target receives no effect.

Let's repeat the steps it takes to create an autocast:

  • Enable the "Autocast" trigger
  • Fill in the global parameters
  • Write an event handler (unless you just apply a buff and use the engine for it)
  • Fill in the parameters in the trigger's comment.

The YouTD engine is coded to be very robust. However, if you mess up some things in your triggers, you can cause harm. The most important things are waits in your event reactions. As soon as you do a wait like calling "TriggerSleepAction" in your event reaction function, you can no longer be sure if your lokal Units are what you suppose them to be. I will start with an example. Let's pretend you want a tower that casts a buff (the buff type is called myBuffType) onto the creep it attacks, but with a delay of two seconds. The first approach would be this one:

function onAttack takes Tower tower returns nothing call TriggerSleepAction(2.0) call myBuffType.apply(tower,Event.getTarget(),tower.getLevel()) endfunction
That this is wrong will be obvious to most of you. The engine cannot ensure (or let's rather say it is VERY UNLIKELY) that Event.getTarget() will return the real target of this event after a wait, since other events that happend in this period will have overwritten it. So you try the next approach by saving Event.getTarget() in a local variable:
function onAttack takes Tower tower returns nothing local Unit enemy = Event.getTarget() call TriggerSleepAction(2.0) call myBuffType.apply(tower,enemy,tower.getLevel()) endfunction
This would look correct to most of you. However, it is still not correct. The problem is that struct indices are reused. Let's pretend this happens in the 2 seconds you wait: The creep "enemy" gets killed. After that a player builds a tower. As soon as the creep dies, its struct id is freed. Since creep and tower share the same base struct Unit, it is possible that the tower will get the freed id of the unit. So in this situation, your trigger would now cast the buff onto the new built tower instead of the attacked creep! This would lead to strange bugs. So the engine allows you to do a safety check on those structs, to check if they are still the same thing that they were when you aquired them. Almost all structs that can be destoryed in the YouTD engine offer this method:
method getUID takes nothing returns integer
It returns a Unique Identifier (UID). This is an integer that is never used twice for two different instances of a struct. So if the struct variable you saved would have changed in that time and you saved its old UID, you would see that it was replaced, because the UIDs wouldn't match. Note that also the UID of a struct changes as soon as it gets destroyed (it becomes 0 or -1). So by comparing a structs UID with its old UID, you can ALWAYS be sure that it is exactly the same struct instance than before and is not destroyed yet., the old and the new UID wouldn't match. So this would be the correct sollution:

function onAttack takes Tower tower returns nothing local Unit enemy = Event.getTarget() local integer uid = enemy.getUID() call TriggerSleepAction(2.0) if uid == enemy.getUID() then call myBuffType.apply(tower,enemy,tower.getLevel()) endif endfunction
So as you see, we save the UID before the wait and then check after the wait if the UID is still the same. That's the way to handle waits. I am sorry that this is a bit cumbersome. However, there was no way to make it easier. This works also for buffs and items (they have also a getUID() method).

One more thing concerning that example: To be 100% correct, we should also verify that the tower is still the same after the wait. The tower may have been sold in that 2 seconds and another one could have been built that took the old tower's id. But if the buff has no effect on the caster but only on the target that is no severe thing and can be skipped. However, if this buff is a buff that grants something to the caster (for example adds mana to the caster periodically) such a check is necessary. So if you really need waits, use the UID for safety reasons, but in most cases, just try to avoid waits where possible!

Now you might wonder: What is with buffs and the .getCaster() method? If you add a periodic event into a buff that does something with its caster, then you would use the .getCaster() method. But what if the caster already died? Wouldn't you then alter another unit? The answer is: No, the engine ensures that this doesn't happen by performing internal UID checks. If the caster of a buff has died, the .getCaster() method will return 0. You can check for .getCaster() != 0 if you want to know if the caster of a buff is still alive.

There are still some uncovered parts of the API that can useful often. The first one is user values that you can set for certain structs.

Often, you want your spells, towers, items or buffs to have some custom data attached to them, that should be kept even between events. For this purpose many structs offer these two members:

//A real number attached to this struct that you can use for whatever you want. //Note that its value is undefined until you define it real userReal //An integer number attached to this tower that you can use for whatever you want. //Note that its value is undefined until you define it integer userInt
These are two numbers, an integer and a real number that you can get and set arbitrarily. Following structs have them:
  • Unit and thus its descendants Creep and Tower, however you should always only use the user Values of your tower, as the user values of other towers and creeps may be used by their abilities!
  • DummyUnit and thus its descendants SpellDummy and Projectile. So you can attach values to your projectiles like counting the damage it already did or such things.
  • Items. Again only use the values of your own item to avoid collisions with other user's values.
  • EventPlacers and thus its decendants Buff, ItemEffect and AuraEffect.
So you can attach an int and a real to your stuff. If you need more variables attached, create your own struct type and then just set the struct as userInt (since structs are integers). Using this, you can add an arbitrary amount of data to your tower/item/dummies/buffs. Note that these members have no specific value unless you define them.

You can get the time elapsed since game start by calling:
Game.getGameTime()
It returns an integer number that measures the time since game start in 1/25 seconds. So if it returns 1000, the game runs for 1000/25 = 40 seconds.